package com.tangye.massageshop.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * MybatisPlusConfig
 * @author TANGYE
 * @2021/1/21 16:00
 */
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {

    // 注册乐观锁插件(旧版：3.0.5)
    /*
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
    return new OptimisticLockerInterceptor();
    }
    */

    //注册分页插件(旧版：3.0.5)
    /*
    启用的原因是因为jdk1.8 Date 日期类线程不安全，使用了LocalDate
    @Bean
    public PaginationInterceptor paginationInterceptor(){
    return new PaginationInterceptor();
    }
    */

    // 注册乐观锁和分页插件(新版：3.4.0)
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); // 乐观锁插件
        // DbType：数据库类型(根据类型获取应使用的分页方言)
        // 分页插件
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        paginationInnerInterceptor.setMaxLimit(200L);// 限制每页最多显示200条记录。
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }
}
